package io.jpress.addon.servicebulletins.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.addon.servicebulletins.model.JpressAddonServicebulletins;
import io.jpress.addon.servicebulletins.model.JpressAddonServicebulletinsUser;
import io.jpress.addon.servicebulletins.service.JpressAddonServicebulletinsService;
import io.jpress.addon.servicebulletins.service.JpressAddonServicebulletinsUserService;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.web.base.AdminControllerBase;

import java.util.List;


@RequestMapping(value = "/admin/servicebulletins/jpress_addon_servicebulletins", viewPath = "/")
public class _JpressAddonServicebulletinsController extends AdminControllerBase {

    @Inject
    private JpressAddonServicebulletinsService service;
    @Inject
    private JpressAddonServicebulletinsUserService suservice;

    @AdminMenu(text = "管理", groupId = "servicebulletins")
    public void index() {
        Columns columns = Columns.create();
        columns.eq("circular_number", getPara("circular_number"))
        .eq("edition", getPara("edition"))
        .eq("title", getPara("title")).or().eq("title_en",getPara("title"))
        .eq("lssuing_unit", getPara("lssuing_unit"));
        Page<JpressAddonServicebulletins> entries=service.paginateByColumns(getPagePara(), 10,columns,"release_date desc");
        setAttr("page", entries);
        render("views/jpress_addon_servicebulletins_list.html");
    }


    public void edit() {
        int entryId = getParaToInt(0, 0);
        JpressAddonServicebulletins entry = entryId > 0 ? service.findById(entryId) : null;
        List<Record> records = Db.find("SELECT id, username, nickname FROM user_admin");
        setAttr("okuser", records);
        List<JpressAddonServicebulletinsUser> servicebulletinsUser = null;
        if(entryId > 0){
            Columns columns = Columns.create()
                    .eq("servicebulletins_id", entryId);
//                    .eq("user_id",getLoginedUser().getId());
            servicebulletinsUser = suservice.findListByColumns(columns);

        }
        setAttr("sbu",servicebulletinsUser);
        setAttr("jpressAddonServicebulletins", entry);
        render("views/jpress_addon_servicebulletins_edit.html");
    }

    @Before(Tx.class)
    public void doSave() {

        String[] userids = getParaValues("userid");
        JpressAddonServicebulletins entry = getModel(JpressAddonServicebulletins.class,
                        "jpressAddonServicebulletins");
        Object id = service.saveOrUpdate(entry);
        Columns columns = Columns.create().eq("servicebulletins_id",id);
        List<JpressAddonServicebulletinsUser> sbus = suservice.findListByColumns(columns);
        if(sbus.size()>0){
            for(JpressAddonServicebulletinsUser sub:sbus){
                suservice.delete(sub);
            }
        }

        for(String userid:userids) {
            JpressAddonServicebulletinsUser sb = new JpressAddonServicebulletinsUser();
            sb.setServicebulletinsId(Long.valueOf(id.toString()));
            sb.setUserId(Long.valueOf(userid));
            suservice.save(sb);
        }

        renderJson(Ret.ok().set("id", entry.getId()));
    }

    public void doDel() {
        Long id = getIdPara();

        Columns columns = Columns.create().eq("servicebulletins_id",id);
        List<JpressAddonServicebulletinsUser> sbus = suservice.findListByColumns(columns);
        if(sbus.size()>0){
            for(JpressAddonServicebulletinsUser sub:sbus){
                suservice.delete(sub);
            }
        }

        render(service.deleteById(id) ? Ret.ok() : Ret.fail());
    }

    @EmptyValidate(@Form(name = "ids"))
    public void doDelByIds() {

        Object[] ids = getParaSet("ids").toArray();

        for(Object o:ids){
            service.deleteById(o);
            Columns columns = Columns.create().eq("servicebulletins_id",o);
            List<JpressAddonServicebulletinsUser> sbus = suservice.findListByColumns(columns);
            if(sbus.size()>0){
                for(JpressAddonServicebulletinsUser sub:sbus){
                    suservice.delete(sub);
                }
            }
        }

        service.batchDeleteByIds(getParaSet("ids").toArray());
        renderOkJson();
    }
}
